package day20170424.homework;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;

import org.junit.Test;

import day20170419.interfaces.Sum;

public class OperationTest {
	
	/**
	 * 运算符测试（灵活版）
	 * @throws Exception
	 */
	@Test
	public void operationTest02() throws Exception {
		File file = new File("txt/data.txt");
		Reader reader = new FileReader(file);
		BufferedReader br = new BufferedReader(reader);
		String rowReader = "";
		while((rowReader = br.readLine()) != null){
			String readLine = rowReader;
			String substring = readLine.substring(0, readLine.length()-1);
			if(substring.contains("+")){
				String[] split = substring.split("\\+");
				int sum = sum(split, "+");
			}else if(substring.contains("-")) {
				
			}
		}
	}
	
	public int sum(String[] split,String fu){
		int number1 = Integer.parseInt(split[0]);
		int number2 = Integer.parseInt(split[1]);
		if("+".equals(fu)){
			return number1 + number2;
		}else if("-".equals(fu)){
			return number1 - number2;
		}else if("*".equals(fu)){
			return number1 * number2;
		}else if("/".equals(fu)){
			return number1 / number2;
		}else if("%".equals(fu)){
			return number1 % number2;
		}
		return -1;
	}
	
	/**
	 * 运算符测试（不够灵活版）
	 * @throws Exception
	 */
	@Test
	public void operationTest01() throws Exception {
		File file = new File("txt/data.txt");
		Reader reader = new FileReader(file);
		BufferedReader br = new BufferedReader(reader);
		int len;
		char[] ch = new char[3];
		String value = "";
		Integer one = 0;
		Integer two = 0;
		Character jieguo = null;
		while((len = br.read(ch)) != -1){
			String str = new String(ch, 0, len);
			char[] charArray = str.toCharArray();
			for(int index=0; index<charArray.length; index++){
				char c = charArray[index];
				if(c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '='){
					String trim = value.trim();
					if(c != '='){
						jieguo = c;
					}
					if(one == 0){
						one = Integer.valueOf(trim);
						value = "";
						trim = "";
					}else{
						two = Integer.valueOf(trim);
						value = "";
						trim = "";
					}
					if(c == '='){
						if(jieguo == '+'){
							System.out.println(one + two);
							one = 0;
							two = 0;
						}else if(jieguo == '-'){
							System.out.println(one - two);
							one = 0;
							two = 0;
						}else if(jieguo == '*'){
							System.out.println(one * two);
							one = 0;
							two = 0;
						}else if(jieguo == '/'){
							System.out.println(one / two);
							one = 0;
							two = 0;
						}else if(jieguo == '%'){
							System.out.println(one % two);
							one = 0;
							two = 0;
						}
					}
				}else{
					if(c != '\0'){
						value = value + charArray[index];
					}
				}
			}
		}
	}
}
